/*
 * Copyright (C) 2024-2025 Deepin Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include "datausagemodel.h"
#include "appdatadetaildialog.h"
#include "widgets/defendertable.h"
#include "widgets/ddialog.h"
#include "widgets/dspinner.h"
//#include <DButtonBox>
//#include <DTipLabel>
//#include <DCommandLinkButton>
//#include <DBackgroundGroup>
//#include <DFrame>
//#include <DSpinner>

#include <QFrame>
#include <QCommandLinkButton>
#include <QPushButton>
#include <QLabel>
#include <QItemDelegate>
#include <QStandardItemModel>
#include <QTableView>


#define FlowWidget_Debug 1

class FlowWidget;
class FlowItemDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    FlowItemDelegate(FlowWidget *widget = nullptr, QObject *parent = nullptr);
    virtual ~FlowItemDelegate() {}

    void paint(QPainter *painter,
               const QStyleOptionViewItem &option,
               const QModelIndex &index) const;

private:
    FlowWidget *m_widget;
};

class NetworkProtectionModel;
class NetworkProtectionWorker;
class FlowWidget : public QWidget
{
    Q_OBJECT
public:
    explicit FlowWidget(DataUsageModel *model = nullptr, QWidget *parent = nullptr);
    ~FlowWidget();
    void setWaitingPage(bool isWaiting);
Q_SIGNALS:

public Q_SLOTS:
    // 异步获取所有进程网速数据
    void asyncGetAllAppFlowListOnTime();
    void onRecAllAppFlowList(const int timeRangeType, const DefenderProcInfoList &flowLst);

private:
    void SetFlowAttributes();
    void updateTable();
    Q_SLOT void showAppFlowDetailDialog();

public slots:
    void dateMenu(QAction *action);
private:
    DataUsageModel *m_model;
    QVBoxLayout *m_layout;
    QLabel *m_totalUsageLabel;
    QPushButton *m_timeRangeSelectBtn;
    QTableView *m_flowtableview;
    QStandardItemModel *m_flowitemmodel;
    // 流量表格模块
    def::widgets::DefenderTable *m_flowTableWidget;
    FlowItemDelegate *m_flowitem;
    // 当前选择显示的流量时间范围
    TimeRangeType m_onDispTimeRangeType;
    // 接收到的流量时间跨度类型
    TimeRangeType m_recedTimeRangeType;
    // 等待界面
    DSpinner *m_waitSpinner;
    // 所有应用流量列表
    DefenderProcInfoList m_allAppFlowList;
};
